﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeGenerator.Ado
{
    public abstract class AdoProviderBase
    {
        /// <summary>
        /// 连接字符串
        /// </summary>
        private string ConnectionString { get; set; }
        /// <summary>
        /// 数据库类型工厂
        /// </summary>
        protected abstract DbProviderFactory DbProvider { get; }
        public AdoProviderBase(string connectionString)
        {
            ConnectionString = connectionString;           
        }

        /// <summary>
        /// 成功要求
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        protected abstract bool SuccessRequire(int index);


        /// <summary>
        /// 测试连接
        /// </summary>
        /// <returns></returns>
        public bool Connection()
        {
            try
            {
                using (var connection = DbProvider.CreateConnection())
                {
                    connection.ConnectionString = ConnectionString;                    
                    connection.Open();
                }
                return true;

            }
            catch (Exception ex)
            {
                Log<AdoProviderBase>.Error($"测试连接报错！{ex.Message};堆栈：{ex.StackTrace}");
                return false;
            }
        }




        protected async Task<FuncResult<int>> ExecuteNonQueryAsync(string sql)
        {
            try
            {
                using (var connection = DbProvider.CreateConnection())
                {
                    connection.ConnectionString = ConnectionString;

                    using (var dbCommand = connection.CreateCommand())
                    {
                        dbCommand.CommandText = sql;
                        dbCommand.CommandType = CommandType.Text;
                        connection.Open();

                        var result = await dbCommand.ExecuteNonQueryAsync();

                        return FuncResult.Success(result);
                    }
                }
            }
            catch (Exception ex)
            {
                Log<AdoProviderBase>.Error($"执行SQL报错！SQL :{sql};{ex.Message};堆栈：{ex.StackTrace}");
                return FuncResult.Fail<int>($"{ex.Message}");
            }
        }


        public FuncResult<DataTable> ExecuteQueryAsync(string sql)
        {
            DataTable table = new DataTable();
            try
            {
                using (var connection = DbProvider.CreateConnection())
                {
                    connection.ConnectionString = ConnectionString;

                    using (var dbCommand = connection.CreateCommand())
                    {
                        dbCommand.CommandText = sql;
                        dbCommand.CommandType = CommandType.Text;
                        connection.Open();

                        using (var dataAdapter = DbProvider.CreateDataAdapter())
                        {
                            dataAdapter.SelectCommand = dbCommand;
                            dataAdapter.Fill(table);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log<AdoProviderBase>.Error($"执行SQL报错！SQL :{sql};{ex.Message};堆栈：{ex.StackTrace}");
                return FuncResult.Fail<DataTable>($"{ex.Message}");
            }
            return FuncResult.Success(table);
        }

    }
}
